<template>
  <div>
    <h1>{{this.id ? '编辑物品': '创建物品'}}</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="物品名称">
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item label="Icon">
      <el-upload
        class="avatar-uploader"
        :action="$http.defaults.baseURL + '/upload'"
        :show-file-list="false"
        :on-success="handleUploadSuccess">
        <img v-if="model.icon" :src="model.icon" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="Category[0]" disabled  style="margin-right:30px">
          <el-option
             v-for="item in Category"
            :key="item._id"
            :label="item.name"
            :value="item._id">
          </el-option>
        </el-select>
        <el-select  @clear="deleteCategory"   clearable v-model="model.category" placeholder="请选择">
          <el-option
            v-for="item in ItemCategory"
            :key="item._id"
            :label="item.name"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
    
  </div>
</template>
<script>
export default {
  name: 'ItemEdit',
  props: {
    id: String
  },
  data () {
    return {
      model: {},
      Category: [],
      ItemCategory: []
    }
  },
  methods: {
    async save () {
      let result
      
      if(this.id){
       
        result = await this.$http.put(`/rest/items/${this.id}`,this.model)
      } else {
        result = await this.$http.post('/rest/items',this.model)
      }
      
      if(result.data){
        this.$router.push('/items/list')
        this.$message({
          type: 'success',
          message: '保存成功'
        })
      }
    },
    async fetch () {
      const res = await this.$http.get(`/rest/items/${this.id}`)
      this.model = res.data
    },
    async fetchCategory () {
      const res = await this.$http.get('/rest/categories',{
        params: {
          $where: "this.parents.length <= 0",
          name: '装备介绍'
        }
      })
      this.Category = res.data
      if(this.Category){
        const children = this.Category[0] && this.Category[0].children
        if(children){
          const childrenRes = await this.$http.get(`/rest/categories/ids/${children}`)
          this.ItemCategory = childrenRes.data
        }
      }
    },
    deleteCategory () {
      this.model.category = null
    },
    handleUploadSuccess (res) {
      res.data.url && this.$set(this.model,'icon',res.data.url)
    }
  },
  created() {
    this.id && this.fetch()
    this.fetchCategory()
  },
}
</script>
<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>